package com.shushan.shushanbao;

import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;

import com.shushan.base.BaseActivity;
import com.shushan.base.Constants;
import com.shushan.base.ScreenManager;
import com.shushan.home.TabHomeActivity;
import com.shushan.login.LoginOrRegisterActivity;
import com.shushan.util.SharedPreferencesUtils;
import com.shushan.pages.VideoPageActivity;
import com.shushan.web.BaseWebActivity;

/**
 * @ClassName: SplashActivity
 * @Desciption: 闪屏
 * @author: yichaohua
 * @date: 2018-05-31
 */
public class SplashActivity extends BaseActivity{

    private static final String TAG = "SplashActivity";

    private String afterOpen;
    private String url;
    private String activity;
    private String extra1;
    private String extra2;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initWindow();

        setContentView(R.layout.splash_activity);

        afterOpen = getIntent().getStringExtra("afterOpen");
        url = getIntent().getStringExtra("url");
        activity = getIntent().getStringExtra("activity");
        extra1 = getIntent().getStringExtra("extra1");
        extra2 = getIntent().getStringExtra("extra2");
        Log.i(TAG,"afterOpen=" + afterOpen + " activity=" + activity + " url=" + url);

        new Handler(new Handler.Callback() {

            @Override
            public boolean handleMessage(Message msg) {

                homeSwitch();

                return false;
            }
        }).sendEmptyMessageDelayed(0,3000);
    }

    private void initWindow() {
        ScreenManager screenManager = ScreenManager.getInstance();
        screenManager.setStatusBar(true, this);
        screenManager.setScreenRoate(true, this);
        screenManager.setFullScreen(true, this);
    }

    private void homeSwitch() {
        boolean appNotFirst = SharedPreferencesUtils.getBooleanDate(Constants.APP_NOT_FIRST_SHAREPREFERS_KEY);
        Log.i(TAG,"appNotFirst=" + appNotFirst);

        if(TextUtils.isEmpty(afterOpen)){
            if(appNotFirst){
                boolean isLogin = SharedPreferencesUtils.getBooleanDate(Constants.IS_LOGIN_SHAREPREFERS_KEY);
                if(isLogin){
                    startActivity(TabHomeActivity.class);
                }else {
                    startActivity(LoginOrRegisterActivity.class);
                }

            }else {
                startActivity(VideoPageActivity.class);
            }
        }else {
            if(afterOpen.equals("go_app")){
                startActivity(TabHomeActivity.class);
            }else if(afterOpen.equals("go_url")){
                if(!TextUtils.isEmpty(url)){
                    Intent intent = new Intent(this, BaseWebActivity.class);
                    intent.putExtra("title",extra1);
                    intent.putExtra("url",url);
                    startActivityAllow(intent);
                }
            }else if(afterOpen.equals("go_activity")){
                if(!TextUtils.isEmpty(activity)){
                    Intent intent = new Intent();
                    intent.setComponent(new ComponentName(this, activity));
                    startActivity(intent);
                }
            }
        }


        onBackAction();
    }
}
